package com.gnerv.battle.boot.project.guard.service.impl;

import com.gnerv.battle.boot.project.guard.datasource.MonitorDatasourceHandler;
import com.gnerv.battle.boot.project.guard.mapper.MonitorDatasourceMapper;
import com.gnerv.battle.boot.project.guard.model.bo.MonitorDatasourceBO;
import com.gnerv.battle.boot.project.guard.model.dto.MonitorDatasourceDTO;
import com.gnerv.battle.boot.project.guard.model.entity.MonitorDatasource;
import com.gnerv.battle.boot.project.guard.service.MonitorDatasourceService;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import static com.gnerv.battle.boot.common.tools.PageTools.limitSize;
import static com.gnerv.battle.boot.common.tools.PageTools.limitStart;

/**
 * @author Gnerv LiGen
 */
@Slf4j
@Service
@AllArgsConstructor
public class MonitorDatasourceServiceImpl extends MonitorDatasourceHandler implements MonitorDatasourceService {
    
    private final MonitorDatasourceMapper monitorDatasourceMapper;

    @Override
    public boolean create(@NonNull MonitorDatasourceBO monitorDatasourceBO) {
        MonitorDatasource monitorDatasource = monitorDatasourceBO.toCreateMonitorDatasource();
        monitorDatasourceMapper.insert(monitorDatasource);
        return true;
    }

    @Override
    public boolean deleteByUkId(@NonNull String ukId) {
        monitorDatasourceMapper.deleteByUkId(ukId);
        return true;
    }

    @Override
    public boolean updateByUkId(@NonNull MonitorDatasourceBO monitorDatasourceBO) {
        MonitorDatasource monitorDatasource = monitorDatasourceBO.toUpdateMonitorDatasource();
        monitorDatasourceMapper.updateByUkId(monitorDatasource);
        return true;
    }

    @Override
    public MonitorDatasourceDTO selectByUkId(@NonNull String ukId) {
        return monitorDatasourceMapper.selectByUkId(ukId);
    }

    @Override
    public List<MonitorDatasourceDTO> listByName(String name, Integer page, Integer size) {
        return monitorDatasourceMapper.listByName(name, limitStart(page, size), limitSize(size));
    }

    @Override
    public boolean testConnection(@NonNull MonitorDatasourceBO monitorDatasourceBO) {
        return test(monitorDatasourceBO);
    }

}
